﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace RemoveFunction
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }

        private void Form3_Load(object sender, EventArgs e)
        {
            comboBox1.DisplayMember = "FunctionCategory";
            comboBox1.ValueMember = "FunctionCategory";
            comboBox1.DataSource = sql.SqlGetDT("select distinct FunctionCategory from FunctionList");
            comboBox1.Text = "常规功能";
            reset();
        }
        void reset() {
            comboBox2.DisplayMember = "FunctionName";
            comboBox2.ValueMember = "FunctionName";
            comboBox2.DataSource = sql.SqlGetDT("select distinct [FunctionName] from FunctionList");
        }
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (textBox1.Text == "")
                {
                    MessageBox.Show("名称不能为空");return;
                }
                    if (textBox2.Text == "") {
                    textBox2.Text = textBox1.Text;
                }
                string sqltext = $@"
            if not exists(select 1 from functionlist where  [FunctionBindingName]='{textBox1.Text}')
            begin 
            DECLARE @last_id NVARCHAR(10) = (select top(1) sort from FunctionList where FunctionCategory='{comboBox1.Text}' order by sort desc);  
            DECLARE @next_id NVARCHAR(10) = (SELECT	left(@last_id,1) + RIGHT('00' + CAST(CAST(RIGHT(@last_id, LEN(@last_id) - 1) AS INT) + 1 AS VARCHAR), 2));
            DECLARE @max_id NVARCHAR(10) = (select max(id) from FunctionList)+1;
            insert into functionlist([ID],[FunctionName],[FunctionCategory],[Sort],[ButtonType],[SuperTip],
            [ShowInRightMenu],[Hotkey],[IsMenu],[CreateTime],[FunctionBindingName]) 
            values(@max_id,'{textBox2.Text}','{comboBox1.Text}',@next_id,1,Null,0,Null,1,CONVERT(varchar, GETDATE(), 121),'{textBox1.Text}')
            end else print 'errer'
            ";
                //insert into FunctionList_XDT select *from functionlist where id = '175'
                if (sql.SqlRevision(sqltext) == 1) {
                    
                    MessageBox.Show("成功！"); reset(); }
                else MessageBox.Show("失败！");
            }
            catch (Exception ex) { MessageBox.Show(ex.ToString()); }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show($"确定删除 [{comboBox2.Text}]? ", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes) {
                string sqltext = $@"Delete From FunctionList where FunctionName='{comboBox2.Text}'";
                if (sql.SqlRevision(sqltext) == 1) { MessageBox.Show("成功！"); reset(); }
                else MessageBox.Show("失败！");
            }
        }
    }
}
